package NIO;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class NioMain {

    public static void main(String[] args) throws IOException {

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        serverSocketChannel.socket().bind(new InetSocketAddress(8080));

        ThreadFactory namedTF = new ThreadFactoryBuilder().setNameFormat("ff-test").build();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 8, 60,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024), namedTF);

        while (true) {
            System.out.println("start listen");
            SocketChannel socketChannel = serverSocketChannel.accept();

            SocketHandler handler = new SocketHandler(socketChannel);
            threadPoolExecutor.execute(handler);
        }
    }
}
